<?php

/**
 * This is the model class for table "equipo".
 *
 * The followings are the available columns in table 'equipo':
 * @property integer $id
 * @property integer $idLaboratorio
 * @property string $nombre
 * @property integer $baja
 *
 * The followings are the available model relations:
 * @property Caracteristica[] $caracteristicas
 * @property Laboratorio $idLaboratorio0
 * @property Experimento[] $experimentos
 * @property Variablexiteracion[] $variablexiteracions
 */
class Equipo extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Equipo the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'equipo';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('idLaboratorio, nombre', 'required'),
			array('idLaboratorio, baja', 'numerical', 'integerOnly'=>true),
			array('nombre', 'length', 'max'=>255),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, idLaboratorio, nombre, baja', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'caracteristicas' => array(self::HAS_MANY, 'Caracteristica', 'idEquipo'),
			'idLaboratorio0' => array(self::BELONGS_TO, 'Laboratorio', 'idLaboratorio'),
			'experimentos' => array(self::MANY_MANY, 'Experimento', 'equipoxexperimento(idEquipo, idExperimento)'),
			'variablexiteracions' => array(self::HAS_MANY, 'Variablexiteracion', 'idEquipo'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'idLaboratorio' => 'Laboratorio',
			'nombre' => 'Nombre',
			'baja' => 'Baja',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('idLaboratorio',$this->idLaboratorio);
		$criteria->compare('nombre',$this->nombre,true);
		$criteria->compare('baja',$this->baja);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
        
        public function getBajaText()
        {
            if($this->baja)
                return "Si";
            else
                return "No";
        }
        
        /**
        * @return array of valid laboratorio for this project, indexed by user IDs
        */
        public function getLaboratorioOptions()
        {
            $laboratorioArray = CHtml::listData(Laboratorio::model()->findAll(), 'id', 'nombre');
            return $laboratorioArray;
        }
        
        /**
        * @return string the status text display for the current issue
        */
        public function getLaboratorioText()
        {
            $laboratorioOptions=$this->laboratorioOptions;
            return isset($laboratorioOptions[$this->idLaboratorio]) ?
            $laboratorioOptions[$this->idLaboratorio] : "unknown status ({$this->idLaboratorio})";
        }
}